package com.haixee.parkdemo.auth.service.impl;

import com.haixee.parkdemo.auth.dao.*;
import com.haixee.parkdemo.auth.entity.dto.GroupOrgDTO;
import com.haixee.parkdemo.auth.enums.JobType;
import com.haixee.parkdemo.auth.model.Group;
import com.haixee.parkdemo.auth.model.Job;
import com.haixee.parkdemo.auth.service.GroupService;
import com.haixee.parkdemo.auth.service.OrganizeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class GroupServiceImpl implements GroupService {

    @Autowired
    private JobMapper jobMapper;

    @Autowired
    private JobCustomMapper jobCustomMapper;

    @Autowired
    private GroupCustomMapper groupCustomMapper;

    @Autowired
    private UserJobCustomMapper userJobCustomMapper;

    @Autowired
    private OrganizeService organizeService;

    @Autowired
    private UserCustomMapper userCustomMapper;

    @Autowired
    private OrganizeCustomMapper organizeCustomMapper;

    @Override
    public void addGroup(String name, Integer parentId, List<Integer> roleIdList) {


        Job job = new Job();
        job.setName(name);
        job.setJobType(JobType.GROUP.getCode());
        jobMapper.insertSelective(job);


        if (roleIdList != null && roleIdList.size() != 0) {
            jobCustomMapper.insertJobRole(job.getId(), roleIdList);
        }

        Group group = new Group();
        group.setId(job.getId());
        group.setName(name);
        group.setParentId(parentId);

        groupCustomMapper.insertSelective(group);
    }

    @Override
    public void addUser(Integer jobId, Integer uid) {
        userJobCustomMapper.insertUserJob(uid, Collections.singletonList(jobId));
    }

    @Override
    public List<Group> getAll() {
        return groupCustomMapper.selectAll();
    }

    @Override
    public void add(Integer groupId, Integer organizeId) {
        groupCustomMapper.insertGroupOrganize(groupId, organizeId);
    }

    @Override
    public List<GroupOrgDTO> getAllGroupOrganize() {

        return groupCustomMapper.getAllOrganize();
    }

    @Override
    public Set<Integer> getUnderTree(List<Integer> jobIdList) {

        // 查询是否是组织的
        List<Job> jobList = jobCustomMapper.selectByIdIn(jobIdList);
        Set<Integer> idList = new HashSet<>();

        for (Job job : jobList) {
            if (job.getJobType() == (byte)1) {
                // 组织关联项目
                List<Integer> allGroupId = new ArrayList<>();
                for (Integer groupId : groupCustomMapper.selectGroupIdByOrgId(job.getId())) {
                    // 遍历添加
                    List<Integer> children = groupCustomMapper.getChildren(groupId);
                    for (Integer child : children) {
                        children.addAll(groupCustomMapper.getChildren(child));
                    }
                    allGroupId.addAll(children);
                }
                idList.addAll(allGroupId);
            } else {
                List<Integer> children = groupCustomMapper.getChildren(job.getId());
                for (Integer child : children) {
                    children.addAll(groupCustomMapper.getChildren(child));
                }
                idList.addAll(children);
            }
        }

        return idList;
    }

    @Override
    public Set<Integer> getUnderTreeByUid(Integer uid) {

        Set<Integer> allGroupId = new HashSet<>();

        // 按组织
        List<Integer> jobIdList = new ArrayList<>();
        jobIdList.addAll(userCustomMapper.selectJobIdByUid(uid));
        for (int i = 0; i < jobIdList.size(); i++) {
            jobIdList.addAll(groupCustomMapper.getChildren(jobIdList.get(i)));
        }
//        allGroupId.addAll(jobIdList);

        // 底下组织权限
        List<Integer> underTree = organizeService.getUnderTree(uid);
        // 添加包含自己的角色
        underTree.addAll(userJobCustomMapper.selectByUidAndType(uid, (byte)1));

        List<Integer> groupIdList = new ArrayList<>();
        for (Integer orgId : underTree) {
            groupIdList.addAll(groupCustomMapper.selectGroupIdByOrgId(orgId));
        }
        // 遍历添加

        for (Integer groupId : groupIdList) {
            List<Integer> children = groupCustomMapper.getChildren(groupId);
            for (Integer child : children) {
                children.addAll(groupCustomMapper.getChildren(child));
            }
            allGroupId.addAll(children);
        }

        allGroupId.addAll(groupIdList);

        return allGroupId;
    }
}
